package com.liujiaqi.controller;

import com.liujiaqi.anno.Log;
import com.liujiaqi.pojo.Dept;
import com.liujiaqi.pojo.Result;
import com.liujiaqi.service.DeptService;
import com.liujiaqi.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 部门管理Controller
*/
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

  @Autowired
  private DeptService deptService;
  @Autowired
  private EmpService empService;

  /*
  * 查询部门数据
  */
  @GetMapping
  public Result list() {
    log.info("查询全部部门数据");

    // 调用service查询部门数据
    List<Dept> deptList = deptService.list();

    return Result.success(deptList);
  }

  /*
  * 删除部门
  */
  @Log
  @DeleteMapping("/{id}")
  public Result delete(@PathVariable Integer id) throws Exception {
    log.info("根据id删除部门：{}",id);
    // 调用service删除部门
    deptService.delete(id);



    return Result.success();
  }

  /*
  * 新增部门
  */
  @Log
  @PostMapping
  public Result add(@RequestBody Dept dept) {
    log.info("新增部门:{}",dept);
    // 调用service新增部门
    deptService.add(dept);
    return Result.success();
  }

  /*
  * 根据ID查询部门信息
  */
  @GetMapping("/{id}")
  public Result search(@PathVariable Integer id) {
    log.info("根据ID查询部门信息：{}", id);
    // 调用service根据id查询部门信息
    Dept dept = deptService.search(id);
    return Result.success(dept);
  }

  /*
  * 编辑部门信息
  */
  @Log
  @PutMapping
  public Result update(@RequestBody Dept dept) {
    log.info("编辑部门信息：{}", dept);
    // 调用service编辑部门信息
    deptService.update(dept.getId(), dept.getName());

    return Result.success();
  }
}
